//
//  NSNotificationCenter+NSNotificationCenterExtend.m
//  etionMisc
//
//  Created by wu jingxing on 13-2-20.
//  Copyright (c) 2013年 GuangZhouXuanWu. All rights reserved.
//

#import "NSNotificationCenterExtend.h"

@implementation NSNotificationCenter (NSNotificationCenterExtend)

+ (void)addObserver:(id)observer dictionarySelector:(NSDictionary *)dicSelector
{
    [NSNotificationCenter addObserver:observer dictionarySelector:dicSelector object:nil];
}

+ (void)addObserver:(id)observer dictionarySelector:(NSDictionary *)dicSelector object:(id)object
{
    [dicSelector enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
    {
        [[NSNotificationCenter defaultCenter] addObserver:observer selector:NSSelectorFromString(obj) name:key object:object];
    }];
}

+ (NSDictionary*)addMainThreadObserver:(id)observer dictionarySelector:(NSDictionary*)dicSelector
{
    return [NSNotificationCenter addMainThreadObserver:observer dictionarySelector:dicSelector object:nil];
}

+ (NSDictionary*)addMainThreadObserver:(id)observer dictionarySelector:(NSDictionary*)dicSelector object:(id)object
{
    NSMutableDictionary *dic = [[NSMutableDictionary new]autorelease_mm];
    
    [dicSelector enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
    {
        id heldID = [[NSNotificationCenter defaultCenter] addObserverForName:key object:object queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
        {
            [observer performSelector:NSSelectorFromString(obj) withObject:note];
        }];
        [dic setObject:heldID forKey:key];
    }];
    
    return dic;
}

+ (void)removeObserver:(id)observer
{
    [NSNotificationCenter removeObserver:observer arrayName:nil object:nil];
}

+ (void)removeObserver:(id)observer name:(NSString*)name object:(id)object
{
    [NSNotificationCenter removeObserver:self arrayName:[NSArray arrayWithObject:name] object:object];
}

+ (void)removeObserver:(id)observer arrayName:(NSArray *)arName object:(id)object
{
    if (arName.count <= 0)
        [[NSNotificationCenter defaultCenter] removeObserver:observer];
    else
    {
        [arName enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        {
            [[NSNotificationCenter defaultCenter] removeObserver:observer name:obj object:object];
        }];
    }
}

+ (void)postNotification:(NSString *)szName object:(id)object
{
    [[NSNotificationCenter defaultCenter] postNotificationName:szName object:object];
}

+ (void)postNotification:(NSString *)szName object:(id)object userInfo:(NSDictionary *)dicUserinfo
{
    [[NSNotificationCenter defaultCenter] postNotificationName:szName object:object userInfo:dicUserinfo];
}

+ (void)postNotification:(NSString *)szName userInfo:(NSDictionary *)dicUserinfo
{
    [[NSNotificationCenter defaultCenter] postNotificationName:szName object:nil userInfo:dicUserinfo];
}

+ (void)postNotification:(NSString *)szName
{
    [[NSNotificationCenter defaultCenter] postNotificationName:szName object:nil userInfo:nil];
}

+ (void)postNotification:(NSString *)szName param:(id)param
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:param forKey:szName];
    [NSNotificationCenter postNotification:szName userInfo:dic];
}

+ (void)postNotification:(NSString*)szName object:(id)object param:(id)param
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:param forKey:szName];
    [NSNotificationCenter postNotification:szName object:object userInfo:dic];
}

@end
